<?php
require("init.php"); //中
$backurl = geturls();   //获取网址
$act = checkstr($_POST["act"]);
switch ($act) {
    case 'modinfo':
        lsf_modinfo();
        break;
    /* case 'doublechk':
        lsf_doublechk();
    break; */
    case 'modpwd':
        lsf_modpwd();
        break;
    case 'modmb':
        lsf_modmb();
        break;
    case 'addressadd':
        lsf_addressadd();
        break;
    case 'addressmodif':
        lsf_addressmodif();
        break;
    case 'zhuan':
        lsf_zhuan();
        break;
    case 'buy_gocoin':
        lsf_buy_gocoin();
        break;
    case 'zhuancoin':
        lsf_zhuancoin();
        break;
    case 'zhuanhuan':
        lsf_zhuanhuan();
        break;
    case 'qianbao':
        lsf_qianbao();
        break;
    case 'transcoin':
        lsf_transcoin();
        break;
    case 'dinggou':
        lsf_dinggou();
        break;
    // case 'open':
    // 	lsf_open();
    // break;
    case 'checkscode':
        check_security_code();
        break;
    case 'checkscode2':
        check_security_code2();
        break;
    case 'yzmbm':
        lsf_yzmbm();
        break;
    case 'yzmb':
        lsf_yzmb();
        break;


}


function check_security_code()
{   //密码验证
    global $db, $GLOBALARR, $r, $adminra, $_userid, $_username, $backurl;
    if ($_POST['checkform_status'] == 'ok') {
        $pwd = checkstr($_POST['pwd']);
        $murl = checkstr($_POST['m']);

        if ($pwd == "") {
            msg_b('请输入密码');
        }

        $pwd = getpwd($pwd);
        if ($r['rgpwd2'] == $pwd) {
            $_SESSION['checkscode'] = $murl;
            locationurl(SITEINDEX . '?lsact=' . $murl);
            exit;
        } else {
            msg_b('密码验证失败');
        }
    }
}

function check_security_code2()
{
    global $db, $GLOBALARR, $r, $adminra, $_userid, $_username, $backurl;
    if ($_POST['checkform_status'] == 'ok') {
        $pwd = checkstr($_POST['pwd']);
        if ($pwd == "") {
            echo json_encode(array('res' => '请输入密码'));
            exit;
        }
        $pwd = getpwd($pwd);
        if ($r['rgpwd2'] == $pwd) {
            $_SESSION['checkscode'] = 'gaiba';
            echo json_encode(array('res' => '验证通过'));
            exit;
        } else {
            echo json_encode(array('res' => '密码验证失败'));
            exit;
        }
    }
}

function lsf_yzmbm()
{
    global $db, $GLOBALARR, $r, $adminra, $_userid, $_username, $backurl;
    if ($_POST['checkform_status'] == 'ok') {
        $pwd = checkstr($_POST['pwd']);
        $mbq = checkstr($_POST['mbq']);
        $mba = checkstr($_POST['mba']);
        $murl = $_POST['m'];

        if ($pwd == "") {
            msg_b('请输入密码');
        }
        if ($mbq == -1) {
            msg_b('请选择密保问题');
        }
        if ($mba == "") {
            msg_b('请输入密保答案');
        }
        $pwd = getpwd($pwd);

        if ($mbq == $r['mbq'] && $mba == $r['rgmba'] && $r['rgpwd2'] == $pwd) {
            $_SESSION['yzmbm'] = $murl;
            locationurl(SITEINDEX . '?lsact=' . $murl);
            exit;
        } else {
            msg_b('密保验证失败');
        }

    }
}

function lsf_yzmb()
{
    global $db, $GLOBALARR, $r, $adminra, $_userid, $_username, $backurl;
    if ($_POST['checkform_status'] == 'ok') {
        $mbq = checkstr($_POST['mbq']);
        $mba = checkstr($_POST['mba']);
        $murl = $_POST['m'];

        if ($mbq == -1) {
            msg_b('请选择密保问题');
        }
        if ($mba == "") {
            msg_b('请输入密保答案');
        }

        if ($mbq == $r['mbq'] && $mba == $r['rgmba']) {
            $_SESSION['yzmb'] = $murl;
            locationurl(SITEINDEX . '?lsact=' . $murl);
            exit;
        } else {
            msg_b('密保验证失败');
        }

    }
}

function lsf_modinfo()
{
    global $db, $GLOBALARR, $_userid, $r, $backurl, $CoinClient;
    if ($r['can_modify_info']) msg_b("您已经被禁止修改资料");
    $arr = array();

    foreach ($_POST['ptd'] as $k => $v) {
        teditinfo($v, checkstr($_POST["$v"]), $arr);
    }

    $sql = getupdatesql($arr, 'ld_user_info', ' id=' . $_userid, 'mbq,sex');
    $db->sitedbexec($sql);

    $bgy_address = trim($_POST['bgy_address']);
    $xfb_address = trim($_POST['xfb_address']);
    $btc_address = trim($_POST['btc_address']);
    $ltc_address = trim($_POST['ltc_address']);
    $realname = checkstr(trim($_POST['real_name']));
    if ($bgy_address) {
        $valid_res = $CoinClient->validateaddress($bgy_address);
        if (!$valid_res['isvalid']) {
            msg_b("不是一个有效的钱包地址");
        }
        if ($valid_res['ismine']) {
            msg_b("你绑定的地址不正确！");
        }
    }
    if ($bgy_address && !$r['bgy_address']) {
        //$db->query("update ld_user_account set modif_bgynum=modif_bgynum+1, bgy_address='".$bgy_address."' where id=".$_userid,4);
        $db->lspre("update ld_user_account set modif_bgynum=modif_bgynum+1,bgy_address=:t1 where id=:t0", 4, array(array(':t0', ':t1'), array($_userid, $bgy_address)));
    }

    if ($xfb_address && !$r['xfb_address']) {
        //$db->query("update ld_user_account set xfb_address='".$xfb_address."' where id=".$_userid,4);
        $db->lspre("update ld_user_account set xfb_address=:t1 where id=:t0", 4, array(array(':t0', ':t1'), array($_userid, $xfb_address)));
    }
    if ($btc_address && !$r['btc_address']) {
        //$db->query("update ld_user_account set xfb_address='".$xfb_address."' where id=".$_userid,4);
        $db->lspre("update ld_user_account set btc_address=:t1 where id=:t0", 4, array(array(':t0', ':t1'), array($_userid, $btc_address)));
    }
    if ($ltc_address && !$r['ltc_address']) {
        //$db->query("update ld_user_account set xfb_address='".$xfb_address."' where id=".$_userid,4);
        $db->lspre("update ld_user_account set ltc_address=:t1 where id=:t0", 4, array(array(':t0', ':t1'), array($_userid, $ltc_address)));
    }
    if ($realname && !$r['real_name']) {
        $db->lspre("update ld_user_info set real_name=:t1 where id=:t0", 4, array(array(':t0', ':t1'), array($_userid, $realname)));
    }

    record_log("修改资料", 0);

    msg_l("修改资料成功!", $backurl . "/home.php?lsact=myinfo");
}

function lsf_modpwd()
{
    global $db, $GLOBALARR, $_userid, $r, $backurl;
    $mtype = checkstr($_POST['mtype']);
    $ypwd = checkstr($_POST['ypwd']);
    $npwd = checkstr($_POST['npwd']);
    $renpwd = checkstr($_POST['renpwd']);

    if (is_null($mtype)) {
        msg_b('请选择密码类型');
    }
    if ($ypwd == '' || (strlen($ypwd) < 8 || strlen($ypwd) > 20)) {
        msg_b('原密码不正确');
    }
    if ($npwd == '' || (strlen($npwd) < 8 || strlen($npwd) > 20)) {
        msg_b('新密码不正确');
    }
    if ($renpwd != $npwd) {
        msg_b('两次输入的新密码不一致');
    }
    if ($ypwd == $npwd) {
        msg_b('新密码不能和原密码一样');
    }
    $ypwd = md5(md5($ypwd . SITEENCRYPT));
    $npwd = md5(md5($npwd . SITEENCRYPT));
    if ($mtype == "rgpwd1") {
        if ($ypwd != $r['rgpwd1']) msg_b('您输入的原密码不正确');
        $db->sitedbexec("update ld_user_info set rgpwd1='$npwd' where id=$_userid");
    } elseif ($mtype == "rgpwd2") {
        if ($ypwd != $r['rgpwd2']) msg_b('您输入的原密码不正确');
        $db->sitedbexec("update ld_user_info set rgpwd2='$npwd' where id=$_userid");
    }
    record_log("修改密码", 0);
    msg_l("修改密码成功 ", $backurl . "/home.php?lsact=modpwd");
}

function lsf_modmb()
{
    global $db, $GLOBALARR, $_userid, $r, $backurl;
    $mbq = checkstr($_POST['mbq']);
    $mba = checkstr($_POST['mba']);
    if (getcharnums($mba) > 25) msg_b("密保答案输入过多内容，请重新输入25个字符以下.");

    if ($mbq == -1) {
        msg_b('请选择密保问题');
    }
    if ($mba == "") {
        msg_b('请输入密保答案');
    }

    $db->sitedbexec("update ld_user_info set mbq=$mbq,rgmba='$mba' where id=$_userid");
    record_log("修改密保", 0);
    msg_l("修改密保成功 ", $backurl . "/home.php?lsact=modmb");
}

function lsf_bankadd()
{
    global $db, $GLOBALARR, $_username, $r, $backurl;
    $arr['bankname'] = checkstr($_POST['bankname']);
    $arr['bankno'] = checkstr($_POST['bankno']);
    $arr['customer_name'] = checkstr($_POST['bankuser']);
    $arr['bankaddress'] = checkstr($_POST['bankaddress']);
    if (empty($arr['bankname'])) msg_b('请输入银行名称');
    if (empty($arr['bankno'])) msg_b('请输入银行卡号');
    if (empty($arr['customer_name'])) msg_b('请输入开户名称');
    if ($arr['customer_name'] != $r["real_name"]) msg_b('开户名称必须填写您注册时的姓名' . $r["real_name"]);
    $db->query("select * from ld_bank where bankno='" . $arr['bankno'] . "'", 2);
    if (!empty($db->rs)) msg_b('该银行卡号已存在');

    $arr['uname'] = $_username;
    $arr['add_time'] = time();
    $db->query("select count(id) as a from ld_bank where uname='$_username'", 2);
    if ($db->rs['a'] >= $GLOBALARR["sybankcount"]) {
        msg_b('亲爱的会员，您已经有' . $db->rs['a'] . '个银行信息,不能再添加了');
    }
    if ($db->rs['a'] == 0) {
        $arr["is_default"] = 1;
    }
    $sql = getinsertsql($arr, "ld_bank");
    $db->sitedbexec($sql);
    record_log("添加银行资料", 0);
    msg_l("添加成功 ", $backurl . "/home.php?lsact=mybank");
}

function lsf_addressadd()
{
    global $db, $GLOBALARR, $_username, $r, $backurl, $_userid;
    $arr['addressname'] = checkstr($_POST['shname']);
    $arr['phone'] = checkstr($_POST['phone']);
    $arr['province_id'] = checkstr($_POST['province_id']);
    $arr['address'] = checkstr($_POST['address']);
    $arr['beizhu'] = checkstr($_POST['beizhu']);
    $arr['adid'] = $_userid;
    $arr['fir_province_id'] = checkstr($_POST['province_id']);
    if (empty($arr['province_id'])) msg_b('请选择省份！');
    if (empty($arr['addressname'])) msg_b('请输入收货人');
    if (empty($arr['phone'])) msg_b('请输入手机号');
    if (empty($arr['address'])) msg_b('请输入收货地址');
    $arr['add_time'] = time();

    $db->query("select count(id) as a from ld_address where adid='$_userid'", 2);
    if ($db->rs['a'] >= 1) {
        msg_b('亲爱的会员，您已经有1个收货地址,不能再添加了');
    }
    if ($db->rs['a'] == 0) {
        $arr["is_default"] = 1;
    }

    $sql = getinsertsql($arr, "ld_address");

    $db->sitedbexec($sql);
    record_log("添加地址", 0);
    msg_l("添加收货地址成功 ", $backurl . "/home.php?lsact=myaddress");
}

function lsf_addressmodif()
{
    global $db, $GLOBALARR, $_username, $r, $backurl, $_userid;
    $id = checkstr($_POST['deid']);
    $addressname = checkstr($_POST['shname']);
    $phone = checkstr($_POST['phone']);
    $province_id = checkstr($_POST['province_id']);
    $address = checkstr($_POST['address']);
    $beizhu = checkstr($_POST['beizhu']);
    $adid = $_userid;

    if (empty($addressname)) msg_b('请输入收货人');
    if (empty($phone)) msg_b('请输入手机号');
    if (empty($address)) msg_b('请输入收货地址');
    $db->query("select * from ld_address where adid='$_userid' and id=" . $id, 2);
    $info = $db->rs;

    if (!$info) {
        msg_b('修改的地址无效');
    }
    $db->query("update ld_address set addressname='" . $addressname . "',phone='" . $phone . "',address='" . $address . "',beizhu='" . $beizhu . "',province_id='" . $province_id . "' where id=" . $id, 4);
    $line = $db->rownums;
    if ($line == 0) {
        msg_b('修改失败');
    }
    record_log("修改地址", 0);
    msg_l("修改收货地址成功 ", $backurl . "/home.php?lsact=myaddress");
}

//转激活币、排单币
function lsf_zhuan()
{
    global $db, $GLOBALARR, $_username, $r, $member, $backurl, $_userid;
    $username = checkstr($_POST['username']);
    $jine = intval(checkstr(trim($_POST['jine'])));
    $tbz = checkstr($_POST['tbz']);
    $coded = checkstr($_POST['coded']);
    $mobile = $r['phone'];
    //转账类型0激活币1排单币
    $ztype = checkstr(trim($_POST['ztype']));
    if ($ztype != 0 && $ztype != 1) msg_b("请选择转账类型");
    if (getcharnums($tbz) > 150) msg_b("转账备注输入过多内容，请重新输入150个字符以下.");

    if (!chkusername($username)) msg_b("您输入的对方帐号不正确");
    if (!is_numeric($jine)) msg_b("您输入的转账金额不是一个数字");
    if ($jine <= 0) msg_b("转账金额必须大于0");
    if (empty($coded)) msg_b("请输入验证码");

    if (!check_ipop_limit("zhuan", 5)) {
        msg_b("请不要频繁提交!");
    }

    //如果转账倍数开关是0
    if ($GLOBALARR['zhuanbs'] == 0) {
        if ($ztype == 1 && $jine < $GLOBALARR['zhuanmin']) msg_b("单次转账最小金额为 " . $GLOBALARR['zhuanmin'] . "");
        if (($jine % $GLOBALARR['zhuanbsz']) != 0) msg_b("转账必须是" . $GLOBALARR['zhuanbsz'] . "的倍数");
    } else {
        if ($ztype == 1 && $jine < $GLOBALARR['zhuanmin']) msg_b("单次转账最小金额为 " . $GLOBALARR['zhuanmin'] . "");
    }

    if ($r['can_transfer_jhb'] > 0) msg_b("您已经被禁止转账\\n\\n请联系管理员查看原因");

    if ($username == $_username) {
        msg_b("不能转给自己");
    }
    //获得对方账号信息
    $rs = $member->user_exists($username);
    if (empty($rs)) {
        msg_b("您输入的对方帐号不存在");
    }


    //消费劵检查双方是否在一条线上
    if ($ztype == 1 && strpos($rs['parents_ids'], "," . $_userid . ",") === false) {
        msg_b("操作不正确！");
    }
    $code = strtolower($_SESSION["mobile_code"]);

    if ($code != $coded || $mobile != $_SESSION['mobile']) {
        msg_b("您输入的验证码不正确");
    }

    $ks = $jine * $GLOBALARR['zhuanks'] / 100;
    $ks = -$ks;
    $qian = $jine + $ks;

    if ($ztype == 0 && $r['cash_purse'] < $jine) {
        msg_b("您的商品积分余额不足" . $jine);
    }

    if ($ztype == 1 && $r['give_help_coin'] < $jine) {
        msg_b("您的消费劵余额不足" . $jine);
    }
    try {
        $db->begintransaction();
        $arr['user_name'] = $_username;
        $arr['uname'] = $username;
        $arr['tran_time'] = time();
        $arr['amount'] = $jine;
        $arr['ks'] = $ks;
        $arr['tbz'] = $tbz;
        $arr['ztype'] = $ztype;

        //写进ld_transfer_accounts表
        $sql = getinsertsql($arr, "ld_transfer_accounts", 'amount,ks,ztype');
        $db->sitedbexec($sql);

        if ($ztype == 0) {
            //写进我的账户，激活币
            writeaccount(array($_userid, "1", 0, time(), 4, $jine, $_username . '转账商品积分给 ' . $username));
            //写进对方账户
            writeaccount(array($rs["id"], "0", 0, time(), 4, $qian, $username . '收到商品积分转账来自于' . $_username));
            record_log($_username . "转账" . $jine . "激活币给" . $username, 0);
        } elseif ($ztype == 1) {
            //排单币
            writeaccount(array($_userid, "1", 8, time(), 5, $jine, $_username . '转账消费劵给 ' . $username));
            writeaccount(array($rs["id"], "0", 8, time(), 5, $qian, $username . '收到消费劵币转账来自于' . $_username));
            record_log($_username . "转账" . $jine . "排单币给" . $username, 0);
        }
        $db->committransaction();
    } catch (PDOException $e) {
        $db->rollbacktransaction();
        a_bck("error");
    }

    msg_l("转账成功 ", $backurl . "/home.php?lsact=zhuanlist");
}

function lsf_zhuancoin()
{
    global $db, $GLOBALARR, $_username, $r, $member, $backurl, $_userid, $CoinClient;
    $coin = checkstr($_POST['coin']);
    if ($coin == 'pbc') {
        $num = checkstr($_POST['num']);
        $num = floor($num * 100) / 100;
        $mobile = $r['phone'];
        $bgy_address = checkstr($_POST['bgy_address']);
        $coded = checkstr($_POST['coded']);
        // $sprice=getprice();
        $sprice = 1;
        //转账类型0本金1利息2动态奖3晋级奖
        $ztype = checkstr(trim($_POST['ztype']));


        if (empty($num)) msg_b("请输入提现数量");
        if (!is_numeric($num)) msg_b("提现数量为数字");
        if (empty($mobile)) msg_b("当前账户未绑定手机");
        if (empty($bgy_address)) msg_b("当前账户未绑定电子钱包地址");
        // if(empty($coded))  msg_b("请输入验证码");
        if (empty($sprice)) msg_b("无法获取VIC价格");
        if ($ztype != 0 && $ztype != 1 && $ztype != 2) msg_b("请选择提现类型");


        if ($num < 20) msg_b("提现数量必须大于等于20");

        $code = strtolower($_SESSION["mobile_code"]);
        if ($code != $coded || $mobile != $_SESSION['mobile']) {
            // msg_b("您输入的验证码不正确");
        }
        $valid_res = $CoinClient->validateaddress($bgy_address);
        if (!$valid_res['isvalid']) {
            msg_b("不是一个有效的钱包地址");
        }
        if ($valid_res['ismine']) {
            //msg_b("你绑定的地址不正确！");
        }

        //查询是否转入本金过
        if ($ztype == 0) {

            if ($GLOBALARR['is_tibj'] == 0) {
                // msg_b(" 暂时无法提现");
            }
            if ($num > $r['cash_purse']) {
                msg_b("锁仓总量余额不足");
            }

        }

        $total = $num * $sprice;

        $feebl = $GLOBALARR['txkbj'];
        $fee = $total * $feebl / 100;

        //最低提现额度
        if ($ztype == 1) {
            if ($num > $r['lixi_crash']) {
                msg_b("锁仓收益余额不足");
            }
        }
        if ($ztype == 2) {
            if ($num > $r['keyong_ldprize']) {
                msg_b("团队收益余额不足");
            }

        }


        $stotal = round($total, 2);  //总价


        //24小时只可以提现一次
        $db->query("select * from ld_transfer_coin where coin='pbc' and cuid=" . $_userid . " order by id desc limit 1", 2);
        $lastti = $db->rs;
        /* if ($lastti && time() - $lastti['ctime'] < 3600) {
             msg_b("24小时只可以提现一次");
         }*/

        //实际兑换到的数量
        $sjnum = round(($total - $fee) / $sprice, 2);
        try {
            $db->begintransaction();
            $arr['cuid'] = $_userid;
            $arr['cuname'] = $_username;
            $arr['num'] = $num;
            $arr['coin'] = $coin;
            $arr['sjnum'] = $sjnum;
            $arr['ctime'] = time();
            $arr['ctype'] = $ztype;
            $arr['caddress'] = $bgy_address;
            $arr['cprice'] = $sprice;
            $arr['ctotal'] = $stotal;
            $arr['cfee'] = $fee;
            $arr['is_cross'] = 0;
            $arr['type'] = 0;
            //写进ld_transfer_accounts表
            $sql = getinsertsql($arr, "ld_transfer_coin", 'ctype');
            $db->query($sql, 5);
            $cid = $db->fval;

            if ($ztype == 0) {
                //写进我的账户减去本金
                writeaccount(array($_userid, "1", 0, time(), 40, $stotal, '兑换VIC扣除VIC' . $stotal));

            } elseif ($ztype == 1) {
                writeaccount(array($_userid, "1", 11, time(), 32, $stotal, '兑换VIC扣除锁仓收益' . $stotal));
                //解冻父级们的动态资产
                $fatherarr = array_reverse(explode(',', rtrim(trim($r['parents_ids'], ','), ',')));
                foreach ($fatherarr as $k => $v) { //给父级解冻相应的动态奖
                    if ($k == 30) {
                        break;
                    }
                    if ($k > 3) {
                        $bili = $GLOBALARR['jjtjj'][3] * 0.01;
                    } else {
                        $bili = $GLOBALARR['jjtjj'][$k] * 0.01;
                    }
                    $keyong = $num * $bili; //解冻金额
                    $fsql = "select * from ld_user_account WHERE id={$v}";
                    $db->query($fsql, 2);
                    $finfo = $db->rs;

                    if ($finfo['cash_ldprize'] > $keyong) {
                        $sql = " keyong_ldprize = keyong_ldprize + {$keyong},cash_ldprize = cash_ldprize - {$keyong}";
                    } elseif ($finfo['cash_ldprize'] <= $keyong) {
                        $keyong_ldprize = $finfo['cash_ldprize'];
                        $sql = " keyong_ldprize = keyong_ldprize + {$keyong_ldprize},cash_ldprize = 0";
                    }
                    $db->sitedbexec("update ld_user_account set " . $sql . " where id={$v}");
                }
            } elseif ($ztype == 2) {
                writeaccount(array($_userid, "1", 9, time(), 34, $stotal, '兑换pbc扣除团队收益' . $stotal));
            }


            $db->committransaction();
        } catch (PDOException $e) {
            $db->rollbacktransaction();
            a_bck("error");
        }


        msg_l("兑换成功，到账需要十几分钟到几天不等，请耐心等待！", $backurl . "/home.php?lsact=zhuancoin");
    } else {
        $num = checkstr($_POST['num']);
        $num = floor($num * 100) / 100;
        $mobile = $r['phone'];
        $bgy_address = $r['bgy_address'];
        $coded = checkstr($_POST['coded']);


        if (empty($num)) msg_b("请输入提现数量");
        if (!is_numeric($num)) msg_b("提现数量为数字");
        if (empty($mobile)) msg_b("当前账户未绑定手机");
        if (empty($bgy_address)) msg_b("请输入电子钱包地址");
        //if(empty($coded))  msg_b("请输入验证码");

        if ($num < 0) msg_b("提现数量必须大于0");

        $code = strtolower($_SESSION["mobile_code"]);
        if ($code != $coded || $mobile != $_SESSION['mobile']) {
            // smsg_b("您输入的验证码不正确");
        }
        $valid_res = $CoinClient->validateaddress($bgy_address);
        if (!$valid_res['isvalid']) {
            msg_b("不是一个有效的钱包地址");
        }
        if ($valid_res['ismine']) {
            msg_b("你绑定的地址不正确！");
        }

        $total = $num;
        //最低提现额度
        $fee = $total * $GLOBALARR['txkbj'] / 100;

        $stotal = round($total, 2);  //总价


        if ($coin == 'btc') {
            if ($r['btc_account'] < $stotal) {
                msg_b("您的BTC余额不足" . $stotal);
            }
        } else {
            if ($r['ltc_account'] < $stotal) {
                msg_b("您的LTC余额不足" . $stotal);
            }
        }

        $db->query("select * from ld_transfer_coin where coin='{$coin}' and cuid=" . $_userid . " order by id desc limit 1", 2);
        $lastti = $db->rs;
        /* if ($lastti && time() - $lastti['ctime'] < 3600) {
             msg_b("24小时只可以提现一次");
         }*/
        //实际兑换到的数量
        $sjnum = round(($total - $fee), 2);
        // var_dump($sjnum);exit;
        try {
            $db->begintransaction();
            $arr['cuid'] = $_userid;
            $arr['cuname'] = $_username;
            $arr['num'] = $num;
            $arr['coin'] = $coin;
            $arr['sjnum'] = $sjnum;
            $arr['ctime'] = time();
            if ($coin == 'btc') {
                $arr['ctype'] = 15;
            } else {
                $arr['ctype'] = 16;
            }
            $arr['caddress'] = $bgy_address;
            $arr['cprice'] = 1;
            $arr['ctotal'] = $stotal;
            $arr['cfee'] = $fee;
            $arr['is_cross'] = 0;
            $arr['type'] = 0;
            // var_dump($arr);exit;
            //写进ld_transfer_accounts表
            $sql = getinsertsql($arr, "ld_transfer_coin", 'ctype');
            $db->query($sql, 5);
            $cid = $db->fval;

            if ($coin == 'btc') {
                writeaccount(array($_userid, "1", 156, time(), 1561, $stotal, 'BTC转出' . $stotal));
            } elseif ($coin == 'ltc') {
                writeaccount(array($_userid, "1", 157, time(), 1571, $stotal, 'LTC转出' . $stotal));
            }
            $db->committransaction();
        } catch (PDOException $e) {
            $db->rollbacktransaction();
            a_bck("error");
        }

        msg_l("兑换成功，到账需要十几分钟到几天不等，请耐心等待！", $backurl . "/home.php?lsact=zhuancoin");

    }
}

function lsf_zhuanhuan()
{
    global $db, $GLOBALARR, $_username, $r, $member, $backurl, $_userid, $CoinClient;
    $num = checkstr($_POST['num']);
    // $num=$num;
    $mobile = $r['phone'];
    $coded = checkstr($_POST['coded']);
    $ztype = checkstr($_POST['ztype']);
    $pbc_price = getprice();
    $btc_price = getprice_btc();
    $ltc_price = getprice_ltc();
    $usd_price = getprice_rate();

    if (empty($num)) msg_b("请输入兑换数量");
    if (!is_numeric($num)) msg_b("兑换数量为数字");
    $pwd = checkstr($_POST['pwd']);
    if ($pwd == "") {
        msg_b('请输入密码');
    }
    $pwd = getpwd($pwd);
    if ($r['rgpwd2'] != $pwd) {
        msg_b('密码验证失败');
    }
    // if(empty($mobile))  msg_b("当前账户未绑定手机");
    // if(empty($bgy_address))  msg_b("请输入电子钱包地址");
    // if(empty($coded))  msg_b("请输入验证码");

    // if($num<1) msg_b("兑换数量必须大于等于1");

    $code = strtolower($_SESSION["mobile_code"]);
    if ($code != $coded || $mobile != $_SESSION['mobile']) {
        // smsg_b("您输入的验证码不正确");
    }
    //   	$valid_res = $CoinClient->validateaddress($bgy_address);
    // if (!$valid_res['isvalid']) {
    // 	msg_b("不是一个有效的钱包地址");
    // 	}
    // if ($valid_res['ismine']) {
    // 		msg_b("你绑定的地址不正确！");
    // }

    $total = $num;
    //最低提现额度
    // $fee=$total*$GLOBALARR['txkbj']/100;

    // $stotal=round($total,2);  //总价
    $stotal = $total;


    if ($ztype == 'btc') {
        if ($r['btc_account'] < $stotal) {
            msg_b("您的BTC余额不足" . $stotal);
        }
    } else {
        if ($r['ltc_account'] < $stotal) {
            msg_b("您的LTC余额不足" . $stotal);
        }
    }

    // $db->query("select * from ld_transfer_coin where coin='{$coin}' and cuid=".$_userid." order by id desc limit 1",2);
    //    $lastti=$db->rs;
    //    if($lastti && time()-$lastti['ctime']<3600 ){
    //    	msg_b("24小时只可以提现一次");
    //    }
    //实际兑换到的数量
    if ($ztype == 'btc') {
        $sjnum = round(($total * $btc_price * $usd_price / $pbc_price), 4);
        $coin = 'btc';
        $cprice = $btc_price;
    } else {
        $sjnum = round(($total * $ltc_price * $usd_price / $pbc_price), 4);
        $coin = 'ltc';
        $cprice = $ltc_price;
    }
    // var_dump($sjnum);exit;
    try {
        $db->begintransaction();
        $arr['cuid'] = $_userid;
        $arr['cuname'] = $_username;
        $arr['num'] = $num;
        $arr['coin'] = $coin;
        $arr['sjnum'] = $sjnum;
        $arr['ctime'] = time();
        $arr['cprice'] = $cprice;
        $arr['pbc_price'] = $pbc_price;
        $arr['rate'] = $usd_price;
        $arr['ctotal'] = $sjnum;
        // $arr['is_cross']=0;
        $arr['type'] = 0;
        $arr['ctype'] = 1;
        // var_dump($arr);exit;
        //写进ld_transfer_accounts表
        $sql = getinsertsql($arr, "ld_zhuanhuan_log", 'ctype');
        $db->query($sql, 5);
        $cid = $db->fval;
        // var_dump($ztype);exit;
        if ($ztype == 'btc') {
            writeaccount(array($_userid, "1", 156, time(), 1561, $stotal, 'BTC兑换VIC:' . $stotal));
            writeaccount(array($_userid, "0", 0, time(), 231, $sjnum, 'BTC兑换VIC:' . $sjnum));
        } elseif ($ztype == 'ltc') {
            writeaccount(array($_userid, "1", 157, time(), 1571, $stotal, 'LTC兑换VIC' . $stotal));
            writeaccount(array($_userid, "0", 0, time(), 231, $sjnum, 'LTC兑换VIC:' . $sjnum));
        }


        $db->committransaction();
    } catch (PDOException $e) {
        $db->rollbacktransaction();
        a_bck("error");
    }

    msg_l("兑换成功！", $backurl . "/home.php?lsact=zhuanhuan");
}

function lsf_qianbao()
{
    global $db, $GLOBALARR, $_username, $r, $member, $backurl, $_userid, $CoinClient;
    $num = checkstr($_POST['num']);
    $num = floor($num * 100) / 100;
    $mobile = $r['phone'];
    $coded = checkstr($_POST['coded']);
    $coin = checkstr($_POST['coin']);
    $name = checkstr($_POST['name']);
    $addr = checkstr($_POST['addr']);

    if (empty($coin)) msg_b("请选择币种");
    if (empty($name)) msg_b("请输入地址名称");
    if (empty($addr)) msg_b("请输入钱包地址");
    $pwd = checkstr($_POST['pwd']);
    if ($pwd == "") {
        msg_b('请输入密码');
    }
    $pwd = getpwd($pwd);
    if ($r['rgpwd2'] != $pwd) {
        msg_b('密码验证失败');
    }

    $code = strtolower($_SESSION["mobile_code"]);
    if ($code != $coded || $mobile != $_SESSION['mobile']) {
        // smsg_b("您输入的验证码不正确");
    }
    //   	$valid_res = $CoinClient->validateaddress($bgy_address);
    // if (!$valid_res['isvalid']) {
    // 	msg_b("不是一个有效的钱包地址");
    // 	}
    // if ($valid_res['ismine']) {
    // 		msg_b("你绑定的地址不正确！");
    // }
    try {
        $db->begintransaction();
        $arr['cuid'] = $_userid;
        $arr['cuname'] = $_username;
        $arr['coin'] = $coin;
        $arr['name'] = $name;
        $arr['addr'] = $addr;
        $arr['ctime'] = time();
        // var_dump($arr);exit;
        //写进ld_transfer_accounts表
        $sql = getinsertsql($arr, "ld_user_qianbao", 'ctype');
        $db->query($sql, 5);
        $cid = $db->fval;


        $db->committransaction();
    } catch (PDOException $e) {
        $db->rollbacktransaction();
        a_bck("error");
    }

    msg_l("添加成功！", $backurl . "/home.php?lsact=qianbao");
}

function lsf_transcoin()
{
    global $db, $GLOBALARR, $_username, $r, $member, $backurl, $_userid;
    $num = checkstr($_POST['num']);
    $num = floor($num * 100) / 100;

    $pwd = checkstr($_POST['pwd']);

    //转账类型0本金1利息2动态奖3晋级奖
    $ztype = checkstr(trim($_POST['ztype']));
    //查询是否存在未完成的预约单子 存在的话就不予许提现

    if (empty($num)) msg_b("请输入转换数量");
    if (!is_numeric($num)) msg_b("转换数量为数字");

    if (empty($pwd)) msg_b("请输入验证码");
    if ($ztype != 1 && $ztype != 2 && $ztype != 3) msg_b("请选择转换类型");


    if ($num < 10) msg_b("转换数量必须大于等于10");

    $total = $num;

    $feebl = $GLOBALARR['zhelecoinbl'];
    $fee = $total * $feebl / 100;

    $stotal = round($total, 2);  //总价


    if ($ztype == 1 && $r['lixi_crash'] < $stotal) {
        msg_b("您的静态钱包余额不足" . $stotal);
    }

    if ($ztype == 2 && $r['cash_ldprize'] < $stotal) {
        msg_b("您的动态钱包余额不足" . $stotal);
    }

    if ($ztype == 3 && $r['rise_crash'] < $stotal) {
        msg_b("您的晋级钱包余额不足" . $stotal);
    }

    $ruinfo = has_out($_userid);
    if (!$ruinfo) {
        msg_b("请先消费排单,再来转换！");
    }
    //搜索已经提现了多少金额
    $ti_num = $ruinfo['out_ti_amount'];
    $zuofei = 0;
    if ($stotal + $ti_num >= $ruinfo['out_in_amount'] * $GLOBALARR['outbs']) {
        //允许上下存在1块钱的误差
        if ($stotal + $ti_num > ($ruinfo['out_in_amount'] * $GLOBALARR['outbs']) - 5 && $stotal + $ti_num < ($ruinfo['out_in_amount'] * $GLOBALARR['outbs']) + 5) {
            $zuofei = 1;

        } else {
            $useamount = $ruinfo['out_in_amount'] * $GLOBALARR['outbs'] - $ti_num;
            $usenum = round($useamount, 2);
            if ($usenum <= 0) {
                $zuofei = 1;
            } else {
                msg_b("你最多转换" . $usenum);
            }
        }
    }


    //实际兑换到的数量
    $sjnum = round(($total - $fee), 2);
    try {
        $db->begintransaction();
        $arr['tuid'] = $_userid;
        $arr['tuname'] = $_username;
        $arr['tnum'] = $num;
        $arr['tsjnum'] = $sjnum;
        $arr['ttime'] = time();
        $arr['ttype'] = $ztype;
        $arr['tfee'] = $feebl;


        //写进ld_transfer_accounts表
        $sql = getinsertsql($arr, "ld_trans_elecoin");
        $db->sitedbexec($sql);


        if ($ztype == 1) {
            writeaccount(array($_userid, "1", 11, time(), 32, $stotal, '转换商品积分扣除利息' . $stotal));
            writeaccount(array($_userid, "0", 0, time(), 12, $sjnum, '利息转换商品积分加' . $sjnum));
        } elseif ($ztype == 2) {
            writeaccount(array($_userid, "1", 9, time(), 34, $stotal, '转换商品积分扣除领导奖' . $stotal));
            writeaccount(array($_userid, "0", 0, time(), 12, $sjnum, '动态转换商品积分加' . $sjnum));
        } elseif ($ztype == 3) {
            writeaccount(array($_userid, "1", 13, time(), 38, $stotal, '转换商品积分扣除晋级钱' . $stotal));
            writeaccount(array($_userid, "0", 0, time(), 12, $sjnum, '晋级转换商品积分加' . $sjnum));
        }
        //是否作废
        if ($zuofei == 1) {
            $hasti = $stotal + $ti_num;
            $db->sitedbexec("update ld_out_rule set out_ti_amount=" . $hasti . ",out_time=" . time() . ",out_type=1 where id=" . $ruinfo['id']);
            //更新表作废

            $db->sitedbexec("update ld_transfer_coin set out_status=" . $ruinfo['id'] . " where out_status=0 and cuid=" . $_userid . "");
            //减去入场金 怎么个减法呢
            if ($ruinfo['out_in_amount'] <= $r['fuxiao_jin']) {
                //减去复消金
                writeaccount(array($r['id'], "1", 21, time(), 217, $ruinfo['out_in_amount'], '转换收益达到一定倍数扣除复消赠送积分' . $ruinfo['id']));
            } else {

                $db->query("select id from ld_give_help where status=0 and is_out=0 and amount=" . $ruinfo['out_in_amount'] . " and uid=" . $_userid . " order by id asc limit 1", 2);
                $out = $db->rs;

                if ($out) {
                    $db->sitedbexec("update ld_give_help set is_out=1 where is_out=0 and id=" . $out['id'] . "");

                } else {
                    //直接把复消赠送积分清除为0
                    writeaccount(array($r['id'], "1", 21, time(), 217, $r['fuxiao_jin'], '转换收益达到一定倍数扣除复消赠送积分' . $ruinfo['id']));
                }
            }

        } else {
            $db->sitedbexec("update ld_out_rule set out_ti_amount=out_ti_amount+" . $stotal . " where id=" . $ruinfo['id']);
        }

        $db->committransaction();
    } catch (PDOException $e) {
        $db->rollbacktransaction();
        a_bck("error");
    }

    msg_l("转换成功！", $backurl . "/home.php?lsact=tranelecoin");
}

function lsf_open()
{
    global $db, $GLOBALARR, $_username, $_userid, $r, $member;
    $kid = checkstr($_POST['kid']);

    $karr = fun_openuser($kid, $_userid, 0);

    foreach ($karr as $k => $v) {
        record_log("开通会员" . $v, 0);
    }

    msg_l("开通会员成功 ", $backurl . "/home.php?lsact=openlist");
}

function lsf_dinggou()
{
    global $db, $GLOBALARR, $_username, $_userid, $r;
    $id = checkstr($_POST['id']);
    $price = checkstr($_POST['price']);
    $num = checkstr($_POST['num']);
    //属性值
    $sx1 = checkstr($_POST['sx1']);
    $sx2 = checkstr($_POST['sx2']);
    $sx3 = checkstr($_POST['sx3']);
    $paytype = intval($_POST['pay']);
    $open = $GLOBALARR['open_dinggou'];
    if ($open == 1) {
        msg_l("暂未开放购买功能", $backurl . "/home.php?lsact=porder&id=" . $id);
    }
    if ($paytype != 1 && $paytype != 2 && $paytype != 3) {
        msg_l("请选择支付方式谢谢合作！", $backurl . "/home.php?lsact=porder&id=" . $id);
    }
    $address = get_address($_userid);
    if (!$address) {
        msg_l("请先添加收货地址！再来购买", $backurl . "/home.php?lsact=myaddress");

    }
    $db->query("select * from ld_product where id=" . $id, 2);
    $info = $db->rs;
    if (!$info) {
        msg_l("该产品已不存在！", $backurl . "/home.php?lsact=productlist");

    }
    if ($num <= 0) {
        msg_l("购买数量不得小于0！", $backurl . "/home.php?lsact=porder&id=" . $id);

    }
    if ($num > $info['num']) {
        msg_l("该产品库存量不足,请重新选择购买数量！", $backurl . "/home.php?lsact=porder&id=" . $id);
    }
    $tot = $info['price'] * $num;
    if ($tot > $r['point_crash']) {
        msg_l("你的积分余额不足", $backurl . "/home.php?lsact=porder&id=" . $id);

    }
    if ($tot <= 0) {
        msg_l("请重新选择你要购买的商品", $backurl . "/home.php?lsact=porder&id=" . $id);
    }
    $xftot = $info['scprice'] * $num;
    try {
        $db->begintransaction();
        //扣除积分
        writeaccount(array($r['id'], "1", 12, time(), 36, $tot, $_username . '购买产品' . $id . '减积分'));
        //减去产品的数量
        $synum = $info['num'] - $num;
        $db->query("update ld_product set num=" . $synum . " where id=" . $id . "", 4);
        //添加记录
        $arr['bid'] = $_userid;
        $arr['bname'] = $_username;
        $arr['btime'] = time();;
        $arr['bpid'] = $id;
        $arr['status'] = 1;
        $arr['bnum'] = $num;
        $arr['bprice'] = $info['price'];
        $arr['bphone'] = $address['phone'];
        $arr['baddress'] = $address['address'];
        $arr['bsname'] = $address['addressname'];
        $arr['bpdname'] = $info['productname'];
        $arr['sbeizhu'] = $address['beizhu'];
        if ($sx1 || $sx2 || $sx3) {
            $arr['sx'] = $sx1 . $sx2 . $sx3;
        }
        $sql = getinsertsql($arr, 'ld_order');
        $db->query($sql, 5);

        $db->committransaction();
    } catch (PDOException $e) {
        $db->rollbacktransaction();
        a_bck("error");
    }
    msg_l("订购成功", $backurl . "/home.php?lsact=productlist");
}

/* function lsf_doublechk(){
	global $db,$GLOBALARR,$_username,$_userid,$r;
	$gacode = checkstr(trim($_POST['ga']));
	$ga_login = 1;

	if (!$gacode) {
		msg_b('请输入验证码!');
	}

	$secret = $_SESSION['secret'];
	if (!$secret) {
		msg_b('验证码已经失效,请刷新网页!');
	}

	$ga = new GoogleAuthenticator();

	if ($ga->verifyCode($secret, $gacode, 1)) {
		$ga_val = $secret . '|' . $ga_login;
		$db->lspre("update ld_user_account set is_ga=1,ga=:t1 where id=:t0",4,array(array(':t0',':t1'),array($_userid,$ga_val)));
		msg_l("操作成功","/home.php?lsact=doublechk");
	}
	else {
		msg_l("操作失败","/home.php?lsact=doublechk");
	}
} */
function lsf_buy_gocoin()
{
    global $db, $GLOBALARR, $_userid, $member;
    $num = checkstr($_POST['jine']);
    $bl = $GLOBALARR['tran_num'];

    if ($GLOBALARR['is_buy'] == 0) {
        msg_l("购买功能已关闭！", $backurl . "/home.php?lsact=buy_gocoin");
    }

    if ($bl <= 0) {
        msg_l("当前兑换比例有误！", $backurl . "/home.php?lsact=buy_gocoin");
    }
    if ($num <= 0) {
        msg_l("购买数量最低1个起！", $backurl . "/home.php?lsact=buy_gocoin");

    }
    if (!check_ipop_limit("buy_gocoin", 5)) {
        msg_l("请不要频繁提交！", $backurl . "/home.php?lsact=buy_gocoin");
    }
    $tot = $num * $bl;
    $buyinfo = $member->getuv1($_userid);
    if ($tot > $buyinfo['cash_purse']) {
        msg_l("你的商品积分余额不足！", $backurl . "/home.php?lsact=buy_gocoin");
    }

    try {
        $db->begintransaction();
        writeaccount(array($_userid, "1", 0, time(), 40, $tot, '购买消费劵扣除商品积分' . $tot));
        writeaccount(array($_userid, "0", 8, time(), 80, $num, '购买消费劵' . $num));
        $arr['buyid'] = $_userid;
        $arr['buyname'] = $buyinfo['uname'];
        $arr['buytime'] = time();;
        $arr['num'] = $num;
        $arr['buytot'] = $tot;
        $sql = getinsertsql($arr, 'ld_buy_coin_log');
        $db->query($sql, 5);

        $db->committransaction();
    } catch (PDOException $e) {
        $db->rollbacktransaction();
        a_bck("error");
    }
    msg_l("购买成功", $backurl . "/home.php?lsact=buy_gocoin");

}

?>

